package com.example.newsdaily.provider;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;

import java.util.List;

public class NavigationPageProvider extends PageSliderProvider {
    private List<Integer> ids;  // 三个布局文件的id
    private AbilitySlice abilitySlice;

    public NavigationPageProvider(List<Integer> ids, AbilitySlice abilitySlice) {
        this.ids = ids;
        this.abilitySlice = abilitySlice;
    }

    @Override
    public int getCount() {
        return ids.size();
    }

    @Override
    // 根据三个布局文件的id，加载三个布局对象，添加到PageSlider容器中
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        Integer id = ids.get(i);
        Component page = LayoutScatter.getInstance(abilitySlice).parse(id, null, false);

        //componentContainer是被渲染的PageSlider对象
        componentContainer.addComponent(page);
        return page;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }
}